
import { _decorator, Component, game, Canvas, sys, view, Node, Camera, screen, View, ResolutionPolicy, profiler } from 'cc';
import { DEBUG } from 'cc/env';
import { Props } from './frame/common/Enums';
import { aa } from './frame/FrameCore';
import OrbitCamera from './ctrl/OrbitCamera';
import { Debug } from './Phyic/core/Debug';
import sceneMgr from './manager/SceneMgr';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    @property(Canvas)
    canvas: Canvas[] = [];
    @property(Camera)
    cameras: Camera[] = [];
    @property(OrbitCamera)
    cam: OrbitCamera = null;

    @property({ group: "LLM", displayOrder: 1 })
    LLMurl = ""
    @property({ group: "LLM", displayOrder: 1 })
    LLMmodel = ""
    @property({ group: "LLM", displayOrder: 1 })
    LLMkey = ""
    @property({ tooltip: "use high performance", group: Props.Setting, displayOrder: 2 })
    highPerformance = false
    @property({ tooltip: "if show profiler", group: Props.Setting, displayOrder: 2 })
    showProfiler = true;
    @property(Node)
    loading: Node = null;



    onLoad() {
        if (!DEBUG) {
            if (sys.platform == sys.Platform.DESKTOP_BROWSER) {
                view.setResolutionPolicy(3);
            }
        }
        this.setting();
        Debug.camera = this.cameras[1]
        aa.global.LLMkey = this.LLMkey;
        aa.global.LLMurl = this.LLMurl;
        aa.global.LLMmodel = this.LLMmodel;
        aa.global.main = this.node;
        aa.global.cameras = this.cameras;
        aa.global.orbitCam = this.cam;
        aa.global.canvas = this.canvas;
        this.node._persistNode = true;
        game.frameRate = 60
        this.rescale();
        screen.on('window-resize', this.rescale, this);
        sceneMgr.createScene();
        this.loading.active = true;


    }
    rescale() {
        const size = view.getVisibleSize();
        console.log("rezied", size.width, size.height)
        view.setDesignResolutionSize(size.width, size.height, ResolutionPolicy.FIXED_HEIGHT)


    }
    onEnable() {
        this.fullScreen();
        this.showProfiler ? profiler.showStats() : profiler.hideStats();



    }
    async fullScreen() {
        if (DEBUG) return;
        if (!sys.isMobile) {
            await screen.requestFullScreen()
            this._resize();
            screen.on('window-resize', this._resize, this);
        }
    }
    _resize() {
        const size = view.getVisibleSize()
        view.setDesignResolutionSize(size.width, size.height, this.isIpad() ? ResolutionPolicy.SHOW_ALL : ResolutionPolicy.EXACT_FIT)


    }

    isIpad(): boolean {
        //@ts-ignore
        const userAgent = navigator.userAgent || navigator.vendor || window.opera;

        // 用于检测旧版iPad
        const isOldIpad = /iPad|Macintosh/i.test(userAgent) && 'ontouchend' in document;

        // 用于检测iPadOS 13及以上版本
        const isIpadOS13OrLater = (function () {
            // iPad上的Safari将模拟macOS来提升网站兼容性
            // 但仅限于桌面版网站模式
            // 在此模式下，我们可以通过检测多点触控手势API来识别iPad
            // 由于iPad有多点触控手势，而Mac没有，所以可以用这个特征来区分
            if (typeof navigator !== 'undefined' && navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1) {
                return true;
            }
            return false;
        })();

        return isOldIpad || isIpadOS13OrLater;
    }

    /**
     * @description: init game UI layers
     * @return {*}
     */


    /* init setting */
    setting() {
        // game.frameRate = 15;
        // if (this.highPerformance) {
        //     game.frameRate = 60;
        // } else {
        //     game.frameRate = 30;
        // }

    }


}

